#!/bin/bash
# This script assumes that the current directory is the base
# directory of the version.
# We are e.g. in /omd/versions/1.2.3i4p5/

CAP_PROGRAMS="bin/mkeventd_open514 lib/nagios/plugins/check_icmp lib/nagios/plugins/check_dhcp lib/cmc/icmpsender lib/cmc/icmpreceiver"

for PROGRAM in $CAP_PROGRAMS; do
  PROGRAM_PATH=$PROGRAM
  if [ -f "$PROGRAM_PATH" ]; then
    chgrp omd "$PROGRAM_PATH"
    chmod 750 "$PROGRAM_PATH"

    if [ "$PROGRAM" = "lib/nagios/plugins/check_icmp" ]; then
        CAP="cap_net_raw+ep"
    elif [ "$PROGRAM" = "lib/nagios/plugins/check_dhcp" ]; then
        CAP="cap_net_raw,cap_net_bind_service=+ep"
    elif [ "$PROGRAM" = "lib/cmc/icmpsender" ]; then
        CAP="cap_net_raw+ep"
    elif [ "$PROGRAM" = "lib/cmc/icmpreceiver" ]; then
        CAP="cap_net_raw+ep"
    elif [ "$PROGRAM" = "bin/mkeventd_open514" ]; then
        CAP="cap_net_bind_service+ep"
    fi

    setcap "$CAP" "$PROGRAM_PATH"
  fi
done
